import type { PlasmoMessaging } from "@plasmohq/messaging"

const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
  const { url } = req.body
  const { width: winWidth, height: winHeight } = await chrome.windows.getLastFocused()
  const [width, height] = [375, 667]
  const [top, left] = [parseInt(String(winHeight / 2 - height / 2)), parseInt(String(winWidth / 2 - width / 2))]
  chrome.windows.create({ url, type: "popup", width, height, top, left, focused: true }, () => window.close())
}

export default handler
